<?php
class Cache
{
	public static function set($filename, $data, $mtime)
	{
		if (is_array($data) && count($data)) {
			$cachestr = '<?php $GLOBALS["' . $filename .'"] = array(';
			$data[$filename . '_mtime'] = $mtime;
			foreach ($data as $key => $value) {
				$cachestr .= '"' . $key . '"=>"' . $value . '",';
			}
			$cachestr .= '"' . $filename . '_mtime"=>"' . $mtime . '"';
			$cachestr .= ');';
			if (file_put_contents(SYS_CACHE_DIR . $filename . '.php', $cachestr)) {
				$GLOBALS[$filename] = $data;
				return true;
			}
			return true;
		}
		return false;
	}
	
	public static function del($filename = false)
	{
		$file = SYS_CACHE_DIR . $filename . '.php';
		
		if ( $filename !== false && file_exists($file)) {
			if (unlink($file)) return true;
			return false;
		} else {
			$handle = opendir(SYS_CACHE_DIR);
			while (false !== ($row = readdir($handle))) {
				if ($row != '.' && $row != '..') {
					$f = SYS_CACHE_DIR . $row;
					if (unlink($f)) continue;
				}
			}
			closedir($handle);
		}
		return false;
	}
	

}